/**
 * FileName: PhotoScanner.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-11-23 下午3:50:19
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-11-23 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.service;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

/**
 * @ClassName: FileScanner
 * @Description:文件扫描
 * @author: Matrix
 * @date: 2012-11-23 下午3:50:19
 * 
 */
public class FileScanner {

	private static String TAG = "filescan";

	public ScannerListener mScannerListener;

	private final static String IGNORE_FOLDER = ".thumbnails";// 忽略

	private List<String> defaultExtension = new ArrayList<String>();

	/**
	 * DFS深度优先文件夹扫描
	 * 
	 * @param directory
	 */

	public FileScanner(ScannerListener scannerListener) {
		mScannerListener = scannerListener;
		defaultExtension.add("jpg");
		defaultExtension.add("JPG");
	}

	/**
	 * sd卡扫描线程
	 * 
	 * @param directory
	 */
	public void startScanning(final File directory,
			final List<String> fileExtension) {
		if (mScannerListener != null) {
			mScannerListener.onSannerStart();
		}
		// long a = System.currentTimeMillis();
		// searchFileByDFS(directory, fileExtension);
		// if (mScannerListener != null) {
		// mScannerListener.onScannerFinish(totle);
		// totle = 0;
		// }
		// long b = System.currentTimeMillis();
		// Log.i(TAG, "本次扫描耗时:" + (b -a)/1000);
		Thread searchFileThread = new Thread() {
			@Override
			public void run() {
				searchFileByDFS(directory, defaultExtension);
				if (mScannerListener != null) {
					mScannerListener.onScannerFinish(fileTotal, hitFileTotal,
							folderTotal);
					fileTotal = 0;
					hitFileTotal = 0;
					folderTotal = 0;
				}
			}
		};
		searchFileThread.setPriority(Thread.MAX_PRIORITY);
		searchFileThread.start();
	}

	private int fileTotal = 0;// 扫描总文件数量
	private int hitFileTotal = 0;// 命中文件数量
	private int folderTotal = 0;// 扫描文件夹数量

	/**
	 * 深度优先(DFS)扫描全盘
	 * 
	 * @param directory
	 * @param fileExtension
	 */
	private void searchFileByDFS(File directory, List<String> fileExtension) {
		String thisFolder = "";
		boolean flag = false;
		File[] allChilds = directory.listFiles();
		if (allChilds == null || allChilds.length == 0) {
			return;
		}
		FilePackage filePkg = null;
		for (File child : allChilds) {
			if (child.isDirectory()) {
				folderTotal++;
				// 如果该目录被忽略,忽略一些不常用的文件夹
				if (child.getName().startsWith(IGNORE_FOLDER)) {
					continue;
				}
				searchFileByDFS(child, fileExtension);
			} else {
				String fileName = child.getName();
				if (fileFilter(fileName, fileExtension)) {// 判断文件是否被命中
					if (flag == false) {
						thisFolder = directory.getName();
						filePkg = new FilePackage();
						filePkg.firstFilePath = child.getAbsolutePath();
						// Log.d(TAG, "=====folder:" + thisFolder + "=====");
						filePkg.pkgName = thisFolder;
						filePkg.pkgAbsPath = directory.getAbsolutePath();

						Log.d(TAG, "=====folder:" + filePkg.pkgAbsPath
								+ "=====");
					} else {
						filePkg.fileCount++;
					}
					// Log.d(TAG, fileName);
					hitFileTotal++;
					flag = true;
				}
				fileTotal++;
			}
		}
		if (filePkg != null && mScannerListener != null) {
			mScannerListener.onScannerNewPkg(filePkg);
		}
	}

	/**
	 * 查询该目录下的所有文件
	 * 
	 * @param parentFolderPath
	 */
	public File[] findFilesByParentFolder(String parentFolderPath) {
		File parent = new File(parentFolderPath);
		File[] files = parent.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String filename) {
				return fileFilter(filename, defaultExtension);
			}
		});
		return files;
	}

	/**
	 * @ClassName: ScannerListener
	 * @Description:sd卡扫描监听
	 * @author: Matrix
	 * @date: 2012-12-10 下午4:31:04
	 * 
	 */
	public interface ScannerListener {

		public void onSannerStart();

		public void onScannerNewPkg(FilePackage filePkg);

		public void onScannerFinish(int fileTotal, int hitFileTotal,
				int folderTotal);
	}

	/**
	 * 根据指定的文件后缀过滤文件
	 * 
	 * @param fileName
	 * @param extensions
	 * @return
	 */
	private boolean fileFilter(String fileName, List<String> extensions) {
		for (String extension : extensions) {
			if (fileName.endsWith(extension)) {
				return true;
			}
		}
		return false;
	}

	public class FilePackage {
		public String pkgName;
		public String firstFilePath;
		public String pkgAbsPath;
		public int fileCount = 1;
	}
}
